package cn.com.pajx.pajx_visitor_v3.utils;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;

import androidx.annotation.RequiresApi;

import cn.com.pajx.pajx_visitor_v3.api.DownLoadApk;
import cn.com.pajx.pajx_visitor_v3.base.BaseActivity;
import cn.com.pajx.pajx_visitor_v3.bean.AppBean;
import cn.com.pajx.pajx_visitor_v3.ui.view.dialog.CommonDialog;
import pub.devrel.easypermissions.EasyPermissions;
import pub.devrel.easypermissions.PermissionRequest;

/**
 * create by ruancw on 2024/2/28
 * description App升级的工具类
 */
public class UpdateUtil {

    private static UpdateUtil updateUtil;

    public static UpdateUtil getInstant() {
        if (updateUtil == null)
            updateUtil = new UpdateUtil();
        return updateUtil;
    }

    /**
     * 更新apk
     */
    public void updateApk(final BaseActivity activity, final AppBean appBean, final PackageManager packageManager) {
        //判断是否是6.0
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            //手机状态权限动态申请
            applyPermission(activity, appBean, packageManager);
        } else {
            DownLoadApk.getInstant().downloadAPK(activity, appBean.getDownload_url(), packageManager);
        }
    }

    /**
     * 判断是否有读取手机状态权限
     */
    public void applyPermission(BaseActivity activity, AppBean appBean, PackageManager packageManager) {
        String[] perms = {Manifest.permission.WRITE_EXTERNAL_STORAGE};
        if (EasyPermissions.hasPermissions(activity, perms)) {
            // 已经申请过权限，获取手机唯一标识
            DownLoadApk.getInstant().downloadAPK(activity, appBean.getDownload_url(), packageManager);
        } else {
            // 没有申请过权限，现在去申请
            EasyPermissions.requestPermissions(
                    new PermissionRequest.Builder(activity, 200, perms)
                            .setRationale("获取手机存储权限,用于App更新")
                            .setPositiveButtonText("允许")
                            .setNegativeButtonText("拒绝")
                            .build());
        }
    }

    /**
     * 安装应用的流程
     */
    public void installProcess(final BaseActivity activity, PackageManager packageManager) {
        boolean haveInstallPermission;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            //先获取是否有安装未知来源应用的权限
            haveInstallPermission = packageManager.canRequestPackageInstalls();
            if (!haveInstallPermission) {
                //没有权限
                new CommonDialog(activity)
                        .setTitle("未知来源权限")
                        .setMessage("安装此应用需要打开未知来源权限，请去设置中开启权限")
                        .setConfirmText("设置")
                        .setChildClickListener(commonDialog -> {
                            startInstallPermissionSettingActivity(activity);
                            commonDialog.dismiss();
                        })
                        .show();
                return;
            }

        }
        //有权限，开始安装应用程序
        DownLoadApk.getInstant().installAPK(activity);
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    private void startInstallPermissionSettingActivity(BaseActivity a) {
        Uri packageURI = Uri.parse("package:" + a.getPackageName());
        //注意这个是8.0新API
        Intent intent = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES, packageURI);
        a.startActivityForResult(intent, 10086);
    }
}
